package com.author.surface;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;

import javax.swing.JToggleButton;
import javax.swing.border.BevelBorder;

import com.author.document.style.StyleChangeNotify;

public class SubscriptAction extends JToggleButton implements ActionListener {
	/** Width and height of the Subscript action button */
	private static final int	SUBSCRIPT_BUTTON_WIDTH	= 30;
	private static final int	SUBSCRIPT_BUTTON_HEIGHT	= 32;

	/**
	 * Font used to display the "X" and "2" letter inside the Subscript action
	 * button
	 */
	private static final Font	subscriptButtonFont		= new Font(
																"Courier New",
																Font.BOLD, 24);

	/**
	 * the following two fields indicate the two characters which are used as
	 * label on the subscript button
	 */
	private static final String	FIRST_CHARACTER			= "X";
	private static final String	SECOND_CHARACTER		= "2";

	/** X and Y coordinate position of the first character */
	private static int			TX						= 4;
	private static int			TY						= 23;

	/** X and Y coordinate position of the second character */
	private static int			TXX						= 16;
	private static int			TYY						= 28;

	/** Current state of the Superscript action button */
	private boolean				currentState			= false;

	/*
	 * The Superscript button needs a reference to the Subscript button. Because
	 * when the Superscript button is clicked we need to ensure that the
	 * Subscript button is unselected if it was in a selected state before and
	 * vice-versa. These two buttons are complementary.
	 */
	protected JToggleButton		otherButton;

	public SubscriptAction() {
		// Set size of the Subscript action button
		setSize(new Dimension(SUBSCRIPT_BUTTON_WIDTH, SUBSCRIPT_BUTTON_HEIGHT));
		setPreferredSize(getSize());
		setEnabled(true);

		// Set border of the subscript button
		setBorder(new BevelBorder(BevelBorder.LOWERED));
		setBorderPainted(false);

		// Set visible properties
		setToolTipText("Subscript");
		setFocusable(false);
		setVisible(true);

		// Make the Subscript button respond to user click action
		addActionListener(this);
	}

	/**
	 * This method will be executed when the UI updater requests that the button
	 * responsible for displaying Subscript is either selected or de-selected
	 */
	public void setSubscript(boolean value) {
		if (value == true) {
			if (!isSelected()) {
				setSelected(true);
				currentState = true;
			}
		}

		if (value == false) {
			if (isSelected()) {
				setSelected(false);
				currentState = false;
			}
		}
	}

	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;

		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		FontRenderContext frc = g2d.getFontRenderContext();

		GlyphVector glyphs1 = subscriptButtonFont.createGlyphVector(frc,
				FIRST_CHARACTER);
		g2d.drawGlyphVector(glyphs1, TX, TY);
		GlyphVector glyphs2 = subscriptButtonFont.createGlyphVector(frc,
				SECOND_CHARACTER);
		g2d.drawGlyphVector(glyphs2, TXX, TYY);
	}

	/**
	 * This method is needed for both the superscript and the subscript buttons
	 * so that they can keep a reference of each other.
	 * 
	 * @param button_
	 */

	public void setSuperscript(JToggleButton button_) {
		otherButton = button_;
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if (event == null) return;
		if (event.getSource() instanceof JToggleButton) {
			SubscriptAction button = (SubscriptAction) event.getSource();
			StyleChangeNotify styleChangeNotify = StyleChangeNotify
					.getStyleChangeNotify();

			if (button.isSelected()) {
				if (button.otherButton.isSelected()) {
					// if the Superscript button is selected then ensure that it
					// becomes unselected once the Subscript button is selected
					button.otherButton.setSelected(false);
					styleChangeNotify.setSuperscript(true, false);
				}

				currentState = true;
				styleChangeNotify.setSubscript(false, true);
			} else {
				currentState = false;
				styleChangeNotify.setSubscript(true, false);
			}
		}
	}
}
